#include <stdio.h>
#include "ex8.h"

#define N 4
#define M 5

void FindSaddle(int arr[N][M]){
    int i,j;
    int r[N]={0};
    int c[M]={0};
    for (i=0;i<N;i++){
        for (j=0;j<M;j++){
            if (arr[i][j]>arr[i][r[i]]) r[i]=j;
            if (arr[i][j]<arr[c[j]][j]) c[j]=i;
        }
    }
    for (i=0;i<N;i++){
        if (c[r[i]]==i){
            printf("a[%d][%d]=%d\n",i,r[i],arr[i][r[i]]);
            j--;
        }
    }
    if (j==M) printf("not exist saddle point.\n");
}

void print_array(int arr[N][M]){
    int i,j;
    for (i=0;i<N;i++){
        for (j=0;j<M;j++){
            printf("%4d",arr[i][j]);
        }
        printf("\n");
    }
}

void ex8(){
    int arr1[N][M] = {0};
    int arr2[N][M] = {{1,2,3,4,11},{2,4,6,8,12},{3,6,9,10,15},{4,8,12,16,7}};
    int i,j;
    for (i=0;i<N;i++){
        for (j=0;j<M;j++){
            arr1[i][j] = i+j+1;
        }
    }
    print_array(arr1);
    FindSaddle(arr1);
    print_array(arr2);
    FindSaddle(arr2);
}